package com.mcintyret.site.journal;

import com.google.gson.annotations.Expose;
import com.mcintyret.site.users.OwnedByUser;

import javax.persistence.*;
import java.util.Date;

/**
 * User: mcintyret2
 * Date: 28/03/2012
 */

@Entity
@Table(name = "datapoints")
public class Datapoint implements OwnedByUser {

    private int id;
    private Timeseries timeseries;
    private String username;
    @Expose
    private double value;
    @Expose
    private Date time;
    @Expose
    private String comment;

    // For use by Hibernate
    Datapoint(){}

    public Datapoint(double value) {
        setValue(value);
        setTime(new Date());
    }

    @Id
    @GeneratedValue
    @Column(name = "datapoint_id")
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Column(name = "value")
    public double getValue() {
        return value;
    }

    public void setValue(double value) {
        this.value = value;
    }

    @Column(name = "username")
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Column(name = "time")
    public Date getTime() {
        return time;
    }

    public void setTime(Date time) {
        this.time= time;
    }

    @Column(name = "comment")
    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    @ManyToOne(optional = false)
    @JoinColumn(name = "numerical_sequence_id")
    public Timeseries getTimeseries() {
        return timeseries;
    }

    public void setTimeseries(Timeseries timeseries) {
        this.timeseries = timeseries;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        Datapoint datapoint = (Datapoint) o;

        if (Double.compare(datapoint.value, value) != 0) {
            return false;
        }
        if (timeseries != null ? !timeseries.equals(datapoint.timeseries) :
                datapoint.timeseries != null) {
            return false;
        }
        if (time != null ? !time.equals(datapoint.time) : datapoint.time != null) {
            return false;
        }
        if (username != null ? !username.equals(datapoint.username) : datapoint.username != null) {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode() {
        int result;
        long temp;
        result = timeseries != null ? timeseries.hashCode() : 0;
        result = 31 * result + (username != null ? username.hashCode() : 0);
        temp = value != +0.0d ? Double.doubleToLongBits(value) : 0L;
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        result = 31 * result + (time != null ? time.hashCode() : 0);
        return result;
    }
}
